#include <stdio.h>

#define ARRAY_SIZE 9

void print_array(int* array, int array_size);
void swap(int* a, int* b);
void quicksort(int *array, int array_size);

int main(void) 
{
    //int array[9] = {3, 2, 7, 9, 8, 6, 4, 1, 5};
    int array[ARRAY_SIZE] = {9,8,7,6,5,4,3,2,1};
    int *array_ptr = array;
    print_array(array_ptr, ARRAY_SIZE);
    quicksort(array_ptr, ARRAY_SIZE);
    print_array(array_ptr, ARRAY_SIZE);
    return 0;
}

void print_array(int* array, int array_size) 
{
    for (int i = 0; i < array_size; i++) {
        printf("%d ", *(array + i));
    }
    printf("\n");
}

void swap(int* a, int* b)
{
    int temp = *a;
    *a = *b;
    *b = temp;
}

void quicksort(int *array, int array_size)
{
    if (array == NULL) {
        printf("Array is null. Return.");
        return;
    }
    if (array_size <= 1) {
        return;
    }
    int i = 0;
    int *pivot = array + array_size - 1;
    for (int j = 0; j < array_size - 1; j++) {
        if (*(array + j) < *pivot) {
            swap(array + i, array + j);
            i++;
        }
    }
    swap(pivot, array + i);
    quicksort(array, i);
    quicksort(array + i + 1, array_size - i - 1);
}
